package com.imooc.controller;

import com.imooc.base.BaseInfoProperties;
import com.imooc.entity.bo.RegisterLoginBo;
import com.imooc.entity.pojo.Users;
import com.imooc.entity.vo.UsersVo;
import com.imooc.grace.result.GraceJSONResult;
import com.imooc.grace.result.ResponseStatusEnum;
import com.imooc.service.UsersService;
import com.imooc.utils.IPUtil;
import com.imooc.utils.SmsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.*;

@RestController
@Api(tags = "PassportController 通信的接口模块")
@RequestMapping("/passport")
@Slf4j
public class PassportController extends BaseInfoProperties {
    @Autowired
    private SmsUtil smsUtil;
    @Autowired
    private UsersService usersService;
    @PostMapping("/getSMSCode")
    @ApiOperation(value = "PassportController通信的接口模块")
    public GraceJSONResult hello(@RequestParam String mobile, HttpServletRequest request){
        if(StringUtils.isBlank(mobile)){
            return GraceJSONResult.ok();
        }
        // TODO 获得user ip
        String userIp = IPUtil.getRequestIp(request);
        redis.setnx60s(MOBILE_SMSCODE+":"+userIp,userIp);
        // TODO  根据用户ip限制用户60秒之内只能获取一次验证码
        smsUtil.SendMessage(mobile);
        return GraceJSONResult.ok("ok");
    }

    /**
     * 用户注册/登录
     * @param registerLoginBo
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "login 登录接口")
    public GraceJSONResult login(@Valid @RequestBody RegisterLoginBo registerLoginBo){
        log.info("registerLoginBo = {}",registerLoginBo);
        // 1.判断验证码是否正确
        String verifyCode = redis.get(MOBILE_SMSCODE + ":" + registerLoginBo.getMobile());
        // 1.1 如果验证码已过期 或 用户输入的验证码与redis验证码不匹配
        if(StringUtils.isBlank(verifyCode)||!verifyCode.equalsIgnoreCase(registerLoginBo.getSmsCode())){
            return GraceJSONResult.errorMsg(ResponseStatusEnum.SMS_CODE_ERROR.msg());
        }
        // 2. 验证码校验成功
        Users user = usersService.findUserIsExist(registerLoginBo.getMobile());
        if(user==null){
            user = usersService.queryOrSaveUserInfo(registerLoginBo.getMobile());
        }
        UsersVo userVo = new UsersVo();
        UUID userToken = UUID.randomUUID();
        // 3. 把userToken 写入redis
        redis.set(REDIS_USER_TOKEN+":"+user.getId(),userToken.toString());
        BeanUtils.copyProperties(user,userVo);
        userVo.setUserToken(userToken.toString());

        // 4. 登录以后删除短信验证码
        redis.del(MOBILE_SMSCODE+":"+registerLoginBo.getMobile());
        return GraceJSONResult.ok(userVo);
    }
    @PostMapping("/logout")
    public GraceJSONResult logout(@RequestParam String userId){
        // 用户退出删除token
        redis.del(REDIS_USER_TOKEN+":"+userId);
        return GraceJSONResult.ok();
    }
}
